package com.df.gamesynctest.entity;

import java.util.Objects;

public class Vector2 {
    public double x;
    public double y;

    public Vector2() {
    }

    public Vector2(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "Vector2{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }

    public Vector2 normalize() {
        Double len = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
        Vector2  v = new Vector2((float) (x/len), (float) (y/len));
        return v;
    }

    public Vector2 multiply(double scalar) {
        return new Vector2(x * scalar, y * scalar);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Vector2 vector2 = (Vector2) o;
        return Double.compare(x, vector2.x) == 0 && Double.compare(y, vector2.y) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }

    public Vector2 lone() {

        return new Vector2(x,y);
    }

    public Vector2 sub(Vector2 target) {

        return new Vector2(x - target.x, y - target.y);
    }

    public Vector2 plus(Vector2 moveLen) {
        return new Vector2(x + moveLen.x, y + moveLen.y);
    }

    public float distance(Vector2 moveTarget) {

        return (float) Math.sqrt(Math.pow(moveTarget.x-x,2)+Math.pow(moveTarget.y-y,2));
    }
}
